package com.rran.study.designpattern.behavioral.chain.factory;

import com.rran.study.designpattern.behavioral.chain.chain.spring.ChainSpringHandler;
import com.rran.study.designpattern.behavioral.chain.enums.ChainAction;
import com.rran.study.designpattern.behavioral.chain.enums.ChainType;
import com.rran.study.designpattern.behavioral.chain.model.ChainContext;
import com.rran.study.designpattern.behavioral.chain.repository.ChainHandlerRepository;

import java.util.List;

/**
 * @author yy
 * @Type ChainHandlerFactory.java
 * @Desc
 * @date 2020/7/20 23:07
 */
public class ChainHandlerFactory {
    public static ChainSpringHandler getChainHeadHandler(ChainContext context){
        return ChainHandlerRepository.getChainHeadHandler(context.getChainType());
    }


    public static ChainAction getChainAction(ChainType chainType, String actionName){
        List<ChainAction> handlerActions = ChainHandlerRepository.getHandlerActions(chainType);
        for(ChainAction action : handlerActions){
            if(action.getName().equals(actionName)){
                return action;
            }
        }
        return null;
    }

    public static ChainSpringHandler getChainSpringHandler(ChainContext context){
        ChainType chainType = context.getChainType();
        String actionName = context.getAction();
        List<ChainSpringHandler> handlerActions = ChainHandlerRepository.getHandlerChain(chainType);
        for(ChainSpringHandler handler : handlerActions){
            if(handler.getAction().getName().equals(actionName)){
                return handler;
            }
        }
        return null;
    }

}
